package com.qingcheng.sms;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonResponse;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author:boy
 * @Date:2019/7/15
 * @description:
 * @ModifiedBy:
 */
@Component
public class SmsMessagerConsumer implements MessageListener {
    @Autowired
    private SmsUtils smsUtils;
    @Value("${smsCode}")
    private String smsCode;
    @Value("${param}")
    private String param;

    public void onMessage(Message message) {
        String str = new String(message.getBody());
        Map map = JSON.parseObject(str, Map.class);
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        System.out.println("receive message" + phone + ":" + code);
        String paramN = param.replace("[value]", code);
        //使用阿里云发送短信
        CommonResponse response = smsUtils.sendSms(phone, smsCode, paramN);
    }
}
